package com.itheima.rabbitmq;

import com.itheima.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumerwork1 {

    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();

        // 创建频道
        Channel channel = connection.createChannel();
        //声明交换机
        channel.exchangeDeclare("fanout_exchange",BuiltinExchangeType.FANOUT);
        //声明队列
        channel.queueDeclare("queque1",true,false,false,null);
        //队列绑定虚拟机
        channel.queueBind("queque1","fanout_exchange","");

        //创建消费者；并设置消息处理
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                    //收到的消息
                    System.out.println("消费者1-接收到的消息为：" + new String(body, "utf-8"));
//                    //确认消息
//                    channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        //监听消息
        /**
         * 参数1：队列名称
         * 参数2：是否自动确认，设置为true为表示消息接收到自动向mq回复接收到了，mq接收到回复会删除消息，设置为false则需要手动确认
         * 参数3：消息接收到后回调
         */
        channel.basicConsume("queque1", true, consumer);
    }
}